﻿@{
    WebSecurity.RequireAuthenticatedUser();

    var db = Database.Open("PhotoGallery");

    var user = db.QuerySingle("SELECT UserId, DisplayName, Bio FROM UserProfiles WHERE UserId = @0", WebSecurity.CurrentUserId);

    Page.Title = "Edit Profile - " + user.DisplayName;

    string displayName = null;
    if (IsPost) {
        displayName = Request["displayName"].Trim();
        if (displayName.IsEmpty()) {
            displayName = User.Identity.Name;
        }
        var bio = Request["bio"].Trim();

        db.Execute("UPDATE UserProfiles SET DisplayName = @0, Bio = @1 WHERE UserId = @2", displayName, bio, WebSecurity.CurrentUserId);

        Response.Redirect(Href("~/User/View",user.UserId));
    }
}
    
<h1>Edit Profile</h1>

<p>
    Welcome back, @user.DisplayName.  You may use the below form to change your display name (shown in comments)
    and biography (shown on your <a href="@Href("~/User/View", WebSecurity.CurrentUserId)">public profile</a>).
</p>

<form method="post" action="">
    <fieldset class="no-legend">
        <legend>Edit Profile</legend>
        <ol>
            <li>
                <label for="displayName">Display name:</label>
                <input type="text" id="displayName" name="displayName" title="Change display name" placeholder="Display name" value="@(displayName ?? user.DisplayName)" />
            </li>
            <li>
                <label for="bio">Bio:</label>
                <textarea type="text" id="bio" name="bio" title="Change bio" rows="4" cols="50" placeholder="Biography">@user.Bio</textarea>
            </li>
        </ol>
        <p class="form-actions">
            <input type="submit" value="Save" title="Save Changes" />
        </p>
    </fieldset>
</form>